﻿using Equin.ApplicationFramework;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Fawcett.Plugins
{
    public partial class ListForm<T> : Form
    {
        private Button sortButton = new Button();

        public ListForm(String Title, T data)
        {
            InitializeComponent();

            this.Text = Title;
            
            CreatingGrid(data);

            dataGridView1.AutoSize = true;
            
            this.AutoSize = true;
        }


        private void CreatingGrid(T data)
        {


            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
            dataGridView1.DataSource = data;
            

        }


        public void ResizeGrid(DataGridView dataGrid)
        {
            int columnWidth = dataGrid.RowHeadersWidth +2;
            int rowHeight = dataGrid.ColumnHeadersHeight + 2;
           

            for (int i = 0; i < dataGrid.ColumnCount; i++)
            {
                if (dataGrid.Columns[i].Visible)
                {
                    columnWidth += dataGrid.Columns[i].Width;
                    columnWidth += ( dataGrid.Columns[i].DividerWidth *2 );

                }
            }

            for (int i = 0; i < dataGrid.RowCount; i++)
            {
                if (dataGrid.Rows[i].Visible)
                {
                    rowHeight += dataGrid.Rows[i].Height;
                    rowHeight += (dataGrid.Rows[i].DividerHeight * 2);

                }
            }

            if (columnWidth > MdiParent.Width )
            {
                columnWidth = MdiParent.Width - 50;
            }
            else
            {
                dataGrid.Width = columnWidth;
                this.Width = dataGrid.Width + 30;
            }

            if (rowHeight > MdiParent.Height)
            {
                dataGrid.Height = dataGrid.Height - 50;
            }
            else
            {
                dataGrid.Height = rowHeight;
                this.Height = dataGrid.Height + 30;
            }

        }

 
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            dataGridView1.Refresh();
                ResizeGrid(dataGridView1);
            
        }


        public static Image Image {
            get
            {
                return Resources.List.ToBitmap();
            }
        }

        private void ListForm_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible) ResizeGrid(dataGridView1); 
        }

        private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
        {
            ResizeGrid(dataGridView1); 
        }

        private void dataGridView1_ColumnHeadersHeightChanged(object sender, EventArgs e)
        {
            ResizeGrid(dataGridView1); 
        }

        private void dataGridView1_RowHeadersWidthChanged(object sender, EventArgs e)
        {
            ResizeGrid(dataGridView1); 
        }

    }

;}
